//
//  WaterFlowLayout.m
//  UICollectionViewFlowLayoutTest
//
//  Created by Rocky on 16/4/15.
//  Copyright © 2016年 Rocky. All rights reserved.
//

#import "WaterFlowLayout.h"
@interface WaterFlowLayout()
{
    NSInteger ITEM_SIZE;
    NSMutableDictionary *columMaxYDic;
}

@end
//
@implementation WaterFlowLayout
//
- (instancetype)init
{
    self=[super init];
    if (self)
    {
        self.colum = 3;
       // self.minimumLineSpacing = 10;
       // self.itemSpacing = 10;
        self.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    }
    
    return self;
}
-(void)prepareLayout
{
    [super prepareLayout];
    ITEM_SIZE = (self.collectionView.frame.size.width - self.sectionInset.left - self.sectionInset.right-10)/self.colum;
    
}
/*
//返回YES，这样当边界改变的时候，-invalidateLayout会自动被发送，才能让layout得到刷新。
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
    return YES;
}*/
//只有算出它 内容显示高度 才能滚动
- (CGSize)collectionViewContentSize
{
    __block NSString * currentColumKey = @"0";
    //遍历找出最高的列
    [columMaxYDic enumerateKeysAndObjectsUsingBlock:^(NSString * key, NSNumber *maxY, BOOL *stop)
    {
        if ([maxY floatValue] > [columMaxYDic[currentColumKey] floatValue]) {
            currentColumKey = key;
        }
    }];
    return CGSizeMake(0, [columMaxYDic[currentColumKey] floatValue]);
}
-(UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    //找出高度最短的栏；
    __block NSString* currentColumKey = @"0";
    [columMaxYDic enumerateKeysAndObjectsUsingBlock:^(NSString * key, NSNumber*maxY, BOOL * stop)
     {
         if ([maxY integerValue] < [columMaxYDic[currentColumKey]integerValue])
         {
             currentColumKey = key;
         }
     }];
    CGFloat height = 0;
    if (self.delegate&&[self.delegate respondsToSelector:@selector(getNewItemHeight:heightForWidth:)])
    {
        //按item宽度做为基准，按等比缩放的计算方法，获取图片的真正高度；
        height = [self.delegate getNewItemHeight:indexPath heightForWidth:ITEM_SIZE];
    }
    NSInteger x = self.sectionInset.left + (self.sectionInset.right + ITEM_SIZE)*[currentColumKey integerValue];
    NSInteger y = [columMaxYDic[currentColumKey]integerValue] + self.sectionInset.bottom;
    //更新高度，为下次做比较准备
    columMaxYDic[currentColumKey] = @(y + height);
    attributes.frame = CGRectMake(x , y, ITEM_SIZE, height);
    return attributes;
}

-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
    columMaxYDic = [NSMutableDictionary dictionary];
    for(NSInteger i = 0;i < self.colum; i++)
    {
        NSString * col = [NSString stringWithFormat:@"%ld",(long)i];
        columMaxYDic[col] = @0;//默认每一栏的高度；
    }
    NSMutableArray* attributes = [NSMutableArray array];
    NSInteger section = [self.collectionView numberOfSections];
    for (NSInteger j=0; j<section; j++)
    {
        NSInteger cellCount = [self.collectionView numberOfItemsInSection:j];
        for (NSInteger i=0 ; i < cellCount; i++)
        {
            NSIndexPath* indexPath = [NSIndexPath indexPathForItem:i inSection:j];
            [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
        }
    }
    return attributes;
}


@end
